/*
 * @Author: Turump 
 * @Date: 2021-04-02 15:55:24 
 * @Last Modified by: Turump
 * @Last Modified time: 2021-04-02 16:02:16
 * 触摸播放音效，可用于通过touch操作的工具工作时的音效播放
 */

import { TaskBase } from "./TaskBase";

const { ccclass, property, menu } = cc._decorator;

@ccclass @menu("任务组件/触摸播放循环音效")
export default class TouchPlayAudioTask extends TaskBase {
    @property({ type: cc.AudioClip, displayName: "音效资源" })
    audioClip: cc.AudioClip = null;

    audioComp: cc.AudioSource = null;

    start() {
        super.start();
        this.audioComp = this.node.addComponent(cc.AudioSource);
        this.audioComp.clip = this.audioClip;
        this.audioComp.loop = true;
        this.audioComp.playOnLoad = false;
        this.audioComp.rewind();
        this.audioComp.pause();
    }

    enableTask() {
        super.enableTask();
        this.node.on(cc.Node.EventType.TOUCH_START, this.touchBegined, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchEnded, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.touchEnded, this);
    }

    stopTask() {
        super.stopTask();
        this.audioComp.stop();
    }

    touchBegined() {
        cc.log(`${this.name} touchBegined`);
        this.audioComp.play()
    }

    touchEnded() {
        this.audioComp.pause();
    }
}
